﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using System.Data;
using System.Collections;


public class AccountController : AbstractJqueryController
{
    public void GetJoinNum()
    {
        AbstractAccount account = NewObject<AbstractAccount>();
        int[] nums = account.GetTodayJoinNum(DateTime.Now);
        TxtJson = ReturnSuccess("", "{\"roomjoinNum\":" + nums[0] + ",\"carportjoinNum\":" + nums[1] + "}");
    }

    public void GetTotalFee()
    {
        AbstractAccount account = NewObject<AbstractAccount>();
        decimal totalfee = account.GetTotalFee(null);
        TxtJson = ReturnSuccess("", "{\"totalfee\":" + totalfee + "}");
    }

    public void GetChargeList()
    {
        AbstractAccount account = NewObject<AbstractAccount>();
        List<AbstractChargeMoney> chargelist = account.GetAccountChargeList(null);
        Hashtable footer = new Hashtable();

        decimal Lastsurplus = chargelist.Sum(x => x.Lastsurplus);
        decimal ReceivableFee = chargelist.Sum(x => x.ReceivableFee);
        decimal FavourFee = chargelist.Sum(x => x.FavourFee);
        decimal RealityFee = chargelist.Sum(x => x.RealityFee);
        decimal Thissurplus = chargelist.Sum(x => x.Thissurplus);

        footer.Add("SrccName", "合计：");
        footer.Add("Lastsurplus", Lastsurplus);
        footer.Add("ReceivableFee", ReceivableFee);
        footer.Add("FavourFee", FavourFee);
        footer.Add("RealityFee", RealityFee);
        footer.Add("Thissurplus", Thissurplus);

        TxtJson = ListToJson(chargelist, footer);
    }

    public void GetItemClassList()
    {
        AbstractAccount account = NewObject<AbstractAccount>();
        DataTable dt = account.GetAccountItemClassData(null);

        AbstractBaseData basedata = NewObject<AbstractBaseData>();
        List<AbstractBaseData> statitem = basedata.GetStatItem();

        List<string> statlist = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (statlist.FindIndex(x => x == dt.Rows[i]["StatCode"].ToString().Trim()) == -1)
            {
                statlist.Add(dt.Rows[i]["StatCode"].ToString().Trim());
            }
        }

        TxtJson = "{\"total\":0,\"rows\":[";
        string str = "";
        decimal RealityFee, backfee, favourfee;
        for (int i = 0; i < statlist.Count; i++)
        {
            int index = (i + 1);
            DataRow[] drs = dt.Select("StatCode='" + statlist[i] + "'");
            RealityFee = drs.Sum(x => Convert.ToDecimal(x["RealityFee"]));
            backfee = drs.Sum(x => Convert.ToDecimal(x["backfee"]));
            favourfee = drs.Sum(x => Convert.ToDecimal(x["favourfee"]));
            if (str == "")
            {
                str = "{\"id\":" + index + ",\"ItemName\":\"" + statitem.Find(x => x.Code == statlist[i]).Name + "\",\"RealityFee\":\"" + RealityFee + "\",\"backfee\":\"" + backfee + "\",\"favourfee\":\"" + favourfee + "\"}";
            }
            else
            {
                str += ",{\"id\":" + index + ",\"ItemName\":\"" + statitem.Find(x => x.Code == statlist[i]).Name + "\",\"RealityFee\":\"" + RealityFee + "\",\"backfee\":\"" + backfee + "\",\"favourfee\":\"" + favourfee + "\"}";
            }

            for (int k = 0; k < drs.Length; k++)
            {
                RealityFee = drs.ToList().FindAll(x => x["ItemCode"].ToString().Trim() == drs[k]["ItemCode"].ToString().Trim()).Sum(x => Convert.ToDecimal(x["RealityFee"]));
                backfee = drs.ToList().FindAll(x => x["ItemCode"].ToString().Trim() == drs[k]["ItemCode"].ToString().Trim()).Sum(x => Convert.ToDecimal(x["backfee"]));
                favourfee = drs.ToList().FindAll(x => x["ItemCode"].ToString().Trim() == drs[k]["ItemCode"].ToString().Trim()).Sum(x => Convert.ToDecimal(x["favourfee"]));
                str += ",{\"id\":" + index.ToString() + (k + 1).ToString() + ",\"ItemName\":\"" + drs[0]["ItemName"].ToString() + "\",\"RealityFee\":\"" + RealityFee + "\",\"backfee\":\"" + backfee + "\",\"favourfee\":\"" + favourfee + "\",\"_parentId\":" + index + "}";
            }
        }

        RealityFee = dt.Select().Sum(x => Convert.ToDecimal(x["RealityFee"]));
        backfee = dt.Select().Sum(x => Convert.ToDecimal(x["backfee"]));
        favourfee = dt.Select().Sum(x => Convert.ToDecimal(x["favourfee"]));

        TxtJson += str + "],\"footer\":[{\"ItemName\":\"合计\",\"RealityFee\":\"" + RealityFee + "\",\"backfee\":\"" + backfee + "\",\"favourfee\":\"" + favourfee + "\"}]}";
    }

    public void SaveAccount()
    {
        try
        {
            int empId = 1;//Convert.ToInt32(sessionData["empId"]);
            decimal Total = Convert.ToDecimal(ParamsData["Total"]);
            decimal BankMoney = Convert.ToDecimal(ParamsData["BankMoney"]);
            decimal Banksurplus = Convert.ToDecimal(ParamsData["Banksurplus"]);

            AbstractAccount account = NewObject<AbstractAccount>();
            int[] nums = account.GetTodayJoinNum(DateTime.Now);

            account.AccountDate = DateTime.Now;
            account.AccountEmpID = empId;
            account.AccountEmpName = CoreFrame.Business.CommonData.GetData("EmpName", empId).ToString();
            account.TotalFee = Total;
            account.BankFee = BankMoney;
            account.SurplusFee = Banksurplus;
            account.RoomjoinNum = nums[0];
            account.CarportjoinNum = nums[1];

            account.SaveAccount(account);

            TxtJson = ReturnSuccess("交款成功！");
        }
        catch
        {
            TxtJson = ReturnSuccess("交款失败！");
        }
    }
}
 
